package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import android.view.View
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.ActivityTransferBinding
import com.play.ballen.databinding.ActivityWithDrawBinding
import com.play.ballen.me.dialog.InputPayPwdDialogUtils
import com.play.ballen.me.dialog.OperateSuccessDialogUtils
import com.play.ballen.me.vm.TransferVM
import com.play.ballen.me.vm.WithDrawVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil

class TransferActivity : BaseStdTitleActivity<TransferVM, ActivityTransferBinding>() {

    private val inputPayPwdDialogUtils by lazy {
        InputPayPwdDialogUtils(mContext)
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, TransferActivity::class.java)
            context.startActivity(starter)
        }
    }

    private val operateSuccessDialogUtils by lazy { OperateSuccessDialogUtils(this) }

    override fun viewModelClass(): Class<TransferVM> {
        return TransferVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_transfer
    }

    override fun initObserver() {
        super.initObserver()

        viewModel.transferInfo.observe(this) {
            it.run {
                dataBinding.edCount.hint =
                    ResourceUtil.getString(R.string.transfer_number_tip, least)
                dataBinding.tvAvailableCount.text =
                    ResourceUtil.getString(R.string.transfer_count_available, money)

                dataBinding.tvDesc.text = memo
            }
        }

        viewModel.actionCommitSuccess.observe(this) {
            operateSuccessDialogUtils.show(ResourceUtil.getString(R.string.transfer_dialog_tip))
            //刷新页面
            viewModel.getTransferInfo()
        }
    }

    override fun onToolbarRightIconClick() {
        MoneyLogActivity.start(
            this,
            ResourceUtil.getString(R.string.transfer_record_title),
            KVConst.MONEY_LOG_TYPE_TRANSFER,
            ""
        )
    }

    override fun initData() {
        viewModel.getTransferInfo()

        val rightIcon = getTitleBarLayout().getRightIcon()
        rightIcon?.visibility = View.VISIBLE
        rightIcon?.setImageResource(R.mipmap.ic_me_recharge_detail)

        dataBinding.tvAll.setOnSingleClickListener tag@{
            if (viewModel.transferInfo.value == null) {
                return@tag
            }
            dataBinding.edCount.setText(viewModel.transferInfo.value!!.money)
        }

        dataBinding.btn.setOnSingleClickListener tag@{
            val address = dataBinding.edAddress.text.toString().trim()
            if (address.isEmpty()) {
                ToastUtils.show(dataBinding.edAddress.hint.toString())
                return@tag
            }

            val count = dataBinding.edCount.text.toString().trim()
            if (count.isEmpty()) {
                ToastUtils.show(dataBinding.edCount.hint)
                return@tag
            }

            inputPayPwdDialogUtils.show { pwd ->
                viewModel.commitTransfer(pwd, address, count)
            }
        }
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun getToolBarTitle(): String = ResourceUtil.getString(R.string.transfer_title)
}